function Self_Select_Question() {
    var _this = this;
    var page;
    _this.obj = null;
    _this.deptId;
    _this.templateArr = [];
    _this.questionIds = [];
    _this.page_size = 10;
    _this.projectTypeNo = $("#projectTypeNo").val();
    _this.projectId = $("#projectId").val();
    _this.projectStatus = $("#projectStatus").val();
    _this.examPermission=$("#examPermission").val();

    // init
    _this.init = function(_page) {
        page = _page;
        // 初始化受训角色树
        _this.initTrainRoleTree();
        _this.initTable();
        _this.initEvent();
    }


    var role_tree_options = {
        zNodes: '',
        rMenu: ''
    };


    _this.setting = {
        view: {
            dblClickExpand: false,
            fontCss: getFontCss
        },
        callback: {
            onClick: function(e,treeId, treeNode){
                _this.onClick(e,treeId, treeNode);
            }
        }
    };

    //点击
    _this.onClick = function(e,treeId, treeNode) {
        if("" != treeNode.roleId) {
            $("#projectRoleId").val(treeNode.id);
        }
        self_select_question.search();
    };


    //初始化树
    _this.initTrainRoleTree = function(){
        var param = {
            "project_id":$("#projectId").val()
        }
        var url =  appPath + '/popup/project_role_tree';
        var result = _this.ajax(url,param);
        var childrenNodes = eval(result)[0].children;
         if(childrenNodes.length == 1 && "-1" == childrenNodes[0].roleId ){
             $("#projectRoleId").val("-1");
             $("#tree").addClass("nav");
             $("#box-body").removeClass("tree_list");
         }

        role_tree_options.zNodes = eval(result);
        $.fn.zTree.init($("#project_role_treeId"), _this.setting, role_tree_options.zNodes);
        //默认选中根节点
        var treeObj = $.fn.zTree.getZTreeObj("project_role_treeId");
        var rootNodes = treeObj.getNodesByFilter(function(node){return node.level==0;});
        var rootNode = rootNodes[0];
        // 默认选中根节点
        treeObj.selectNode(rootNode);

    };


    _this.initTable = function() {
        var list_url = appPath + "/popup/project_question_list";
        page.init("select_question_form", list_url, "select_question_list", "select_question_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                _this.questionIds.push(item['intId']);
                inner += '<tr >';
                inner += '<td width="50">' + (i+1) + '</td>';
                inner += '<td width="50"><input type=\"checkbox\" name="questionBox" value="'+item['intId']+'" data-id="' + item['intId'] + '"></td>';
                inner += '<td  width="100">'+ item['varNo'] + '</td>';
                var varContent = item['varContent'];
                if(varContent == '' || varContent == undefined || varContent == null){
                    inner += "<td >/</td>";
                }else{
                    inner += '<td ><span class="text-orange tooltip" data-length="15">'+ item['varContent'] + '</span></td>';
                }

                inner += '<td width="100">'+ item['chrType'] + '</td>';
                //inner += '<td width="100">'+ TimeUtil.getHouAndMinAndSec(item['class_hour']*60) + '</td>';
                inner += '<td width="100">'+ item['chrCategory'] + '</td>';
                inner += '</tr>';
            }
            return inner;
        }

    };


    //搜索
    _this.search=function(){
        $("#questionName").val($.trim($("#questionName").val()));
        _this.initTable();
    }


    _this.initEvent = function(){

        //弹窗-选择课程
        $("#btn_dialog_question").click(function(){
            //默认选中根节点
            var treeObj = $.fn.zTree.getZTreeObj("project_role_treeId");
            var childrenNodes = treeObj.getNodes()[0].children;
            var treeNode = treeObj.getSelectedNodes()[0];
            if("" == treeNode.roleId && !(childrenNodes.length == 1 && "-1" == childrenNodes[0].roleId)){
                    layer.msg("请选择受训角色");
                    return;
            }

            // 调用选择培训课程的弹窗
            layer.open({
                type : 2,
                title : '选择试题',
                area : [ '1200px', '80%' ],		//弹出层大小
                scrollbar : false,				//false隐藏滑动块
                content : [ appPath + '/popup/enterSelectQuestion?projectId='+ $("#projectId").val() + "&roleId=" + $("#projectRoleId").val(), 'yes' ]
            });

        });

        // 根据课程名称查询
        $("#questionName_search").click(function(){
            $("#questionName").val($.trim($("#questionName").val()));
            _this.initTable();
        });

        //查询所有
        $("#question_all").click(function(){
            $("#questionName").val('');
            _this.initTable();
        });

        // 批量删除课程
        $("#btn_delete_batch_question").click(function(){
            if(!$(this).hasClass("unclick")){
                _this.project_delete_batch_question();
            }
        });

        //全选课程
        $("#checkAll").click(function () {
            var flag=$("#checkAll").prop("checked");
            $('#select_question_list input[type="checkbox"]').prop("checked",flag);
        })


        //回到首页
        $("#index").click(function () {
            window.location.href = appPath + "/admin";
        })

        //上一步操作
        $("#pev").click(function () {
            // 不限定抽考 没有第三步
            if('3' == $("#isCenterTrain").val()){
                window.location.href=appPath + "/admin/project_create/private/step2?projectTypeNo="+self_select_question.projectTypeNo+"&projectId="+self_select_question.projectId+"&projectStatus="+self_select_question.projectStatus+"&examPermission="+self_select_question.examPermission+"&isCenterTrain="+$("#isCenterTrain").val();
            }else{
                window.location.href=appPath+"/admin/project_create/private/step3?projectTypeNo="+self_select_question.projectTypeNo+"&projectId="+self_select_question.projectId+"&projectStatus="+self_select_question.projectStatus+"&examPermission="+self_select_question.examPermission+"&isCenterTrain="+$("#isCenterTrain").val();
            }
        })


        //下一步操作
        $("#next").click(function () {
            window.location.href = appPath + "/popup/project_create/step6?projectTypeNo="+self_select_question.projectTypeNo+"&source=selectQuestion&projectId="+self_select_question.projectId+"&projectStatus="+self_select_question.projectStatus+"&examPermission="+self_select_question.examPermission+"&isCenterTrain="+$("#isCenterTrain").val();
        });
    }


    // 选择培训课程的回调
    _this.submitQuestion = function(questionIds){
        if(questionIds.length == 0){
            return;
        };

        // 选择培训课程
        $.ajax({
            url: appPath + '/popup/save_project_question',
            async: false,
            type: 'post',
            data: {
                'projectId': $("#projectId").val(),                                  // 项目id
                'roleId': $("#projectRoleId").val(),                                 // 受训角色id
                'questionIds': questionIds.join(",")                                            // 试题ids
            },
            success: function(data){
                var code = data['code'];
                if(code==10000){
                    _this.initTable();
                }else{
                    layer.alert('操作失败', {icon: 2,  skin: 'layer-ext-moon'});
                }
            }
        });
    }



    // 批量删除课程
    _this.project_delete_batch_question = function(){
        if(_this.projectStatus == '2'){
            layer.msg("若要删除试题，请先在首页取消发布本项目！");return;
        }
        var questionIds = [];
        $('#select_question_list input:checked').each(function(i,e){
            var intId = $(this).attr("data-id");
            questionIds.push(intId);
        })

        if(questionIds.length==0){
            layer.msg("未选择试题");return;
        }

        var arr_ = [];
        arr_.push("-1");
        layer.confirm("确定执行此操作?", {
            icon : 3,
            btn : [ "确认", "取消" ]
        }, function() {
            $.ajax({
                url: appPath + '/popup/delete_project_question',
                async: false,
                type: 'post',
                data: {
                    'projectId': $("#projectId").val(),                                  // 项目id
                    'roleId': $("#projectRoleId").val(),                                 // 受训角色id
                    'questionIds': questionIds.join(",")
                },
                success: function(data){
                    var code=data['code']
                    if(code==10000){
                        //layer.alert('操作成功', {icon: 1,  skin: 'layer-ext-moon'}, function(index){
                        layer.closeAll();
                        $("#checkAll").prop("checked",false);
                        // step2.project_role_tree_node_course();
                        _this.initTable();
                        // });

                    }else{
                        layer.alert('操作失敗', {icon: 2,  skin: 'layer-ext-moon'});
                    }
                }
            });
        });
    };





    _this.ajax = function(url, param, type){
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                result = data.result;
            }
        });
        return result;
    };
}

var self_select_question = new Self_Select_Question();

//搜索高亮
function getFontCss(treeId, treeNode){
    return (!!treeNode.highlight)?{'color': "#A60000", 'font-weight':"bold"}:{'color': "#333", 'font-weight':"normal"};
}
